package com.yuncheng.spcyApi.spcy;

import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import org.apache.commons.lang3.StringUtils;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyKpNdkpBasic;
import com.yuncheng.spcyApi.entity.SpcyRsJcysfbgjl;
import com.yuncheng.spcyApi.entity.SpcySqsxXpjl;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.spcyService.SpcyMyInfoService;
import com.yuncheng.spcyApi.spcyService.ZdyUploadFileService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.DateConversionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "审评查验-检查员个人信息相关接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcyMyInfo")
public class SpcyMyInfoController {

    @Resource
    private SpcyMyInfoService spcyMyInfoService;

    @Resource
    private ZdyUploadFileService zdyUploadFileService;

    @Resource
    private ISysUserPxjlService sysUserPxjlService;

    @Resource
    private ISpcySqsxXpjlService spcySqsxXpjlService;

    @Resource
    private ISpcyKpNdkpBasicService spcyKpNdkpBasicService;

    @Resource
    private ISpcyRsJcysfbgjlService spcyRsJcysfbgjlService;

    @Log(title = "获取我的基本信息数据")
    @ApiOperation(value = "获取我的基本信息数据")
    @GetMapping(value = "queryMyJbxx")
    public HttpResult<?> queryMyJbxx(@RequestParam(value = "fuserid",defaultValue = "")String fuserid){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("登录已失效，请重新登录");
        }
        String userid = "";
        if (StringUtils.isBlank(fuserid)) {
            userid = currentUser.getId();
        }else {
            userid = fuserid;
        }
        Map map = new HashMap();

        map = spcyMyInfoService.queryMyJbxx(userid);
        return HttpResult.ok(map);
    }

    @Log(title = "app用户基本信息")
    @ApiOperation(value = "app用户基本信息")
    @GetMapping(value = "appUserInfo")
    public HttpResult queryAppUserInfo(){

        return HttpResult.ok(spcyMyInfoService.selectAppMyInfo(GetCurrentUserUtils.getCurrentUserId()));
    }

    @Log(title = "查询用户所有的检查员身份")
    @ApiOperation(value = "查询用户所有的检查员身份")
    @GetMapping(value = "queryUserJcySf")
    public HttpResult queryUserJcySf(@RequestParam(value = "fuserid",defaultValue = "")String fuserid){
        if (StringUtils.isBlank(fuserid)){
            fuserid = GetCurrentUserUtils.getCurrentUserId();
        }
        return HttpResult.ok(spcyMyInfoService.getUserJcysfList(fuserid));
    }

    @Log(title = "上传-用户照片")
    @ApiOperation(value = "上传-用户照片")
    @ApiImplicitParam(name = "fuserid",value = "用户id",required = true)
    @PostMapping(value = "uploadMyPhoto")
    public HttpResult<?> uploadMyPhoto(@RequestParam("file") MultipartFile file,
                                    @RequestParam(value = "fuserid",defaultValue = "")String id){

        zdyUploadFileService.uploadMyPhoto(file, id);
        return HttpResult.ok("上传成功");
    }

    @Log(title = "上传-检查员证件")
    @ApiOperation(value = "上传-检查员证件")
    @ApiImplicitParam(name = "fuserid",value = "用户id",required = true)
    @PostMapping(value = "uploadMyJcyZj")
    public HttpResult<?> uploadMyJcyZj(@RequestParam("file") MultipartFile file,
                                       @RequestParam(value = "fuserid",defaultValue = "")String id){

        zdyUploadFileService.uploadMyJcyZj(file, id);
        return HttpResult.ok("上传成功");
    }

    @Log(title = "下载-用户照片")
    @ApiOperation(value = "下载-用户照片")
    @ApiImplicitParam(name = "id",value = "用户id")
    @GetMapping(value = "downMyPhoto")
    public void downMyPhoto(@RequestParam(value = "fuserid",defaultValue = "")String id, HttpServletResponse response){
        zdyUploadFileService.downMyPhoto(id, response);
    }

    @Log(title = "下载-检查员证件")
    @ApiOperation(value = "下载-检查员证件")
    @ApiImplicitParam(name = "id",value = "用户id")
    @GetMapping(value = "downMyJcyZj")
    public void downMyJcyZj(@RequestParam(value = "fuserid",defaultValue = "")String id, HttpServletResponse response){
        zdyUploadFileService.downMyJcyZj(id, response);
    }

    @Log(title = "获取我的用户签名照片链接")
    @ApiOperation(value = "获取我的用户签名照片链接")
    @GetMapping(value = "getUserSignPhotoLink")
    public HttpResult getUserSignPhotoLink(@RequestParam(value = "fuserid",defaultValue = "")String fuserid){
        Map map = spcyMyInfoService.getUserSignPhotoLink(fuserid);

        return HttpResult.ok(map);
    }

    @ApiOperation(value = "获取多用户签名链接列表")
    @GetMapping(value = "getMoreUserSignLinkList")
    public HttpResult getMoreUserSignLinkList(@RequestParam(value = "ids",defaultValue = "")String ids){

        return HttpResult.ok(spcyMyInfoService.getMoreUserSignLinkList(ids));
    }

    @ApiOperation(value = "年度选派及检查情况")
    @GetMapping(value = "getJcyYearJcqk")
    public HttpResult getJcyYearJcqk(@RequestParam(value = "fuserid",defaultValue = "")String fuserid){
        if (StringUtils.isBlank(fuserid)){
            fuserid = GetCurrentUserUtils.getCurrentUserId();
        }
        Map resultMap = spcySqsxXpjlService.getJcyYearJcqk(fuserid, DateConversionUtils.DateToYear(new Date()));
        resultMap.put("hours", sysUserPxjlService.getTotalLearningHoursTheCurrentYear(fuserid, DateConversionUtils.DateToYear(new Date())));
        return HttpResult.ok(resultMap);
    }

    @ApiOperation(value = "检查日程")
    @GetMapping(value = "getJcrc")
    public HttpResult getJcrc(@RequestParam(value = "fuserid",defaultValue = "")String fuserid,
                              @RequestParam(value = "nowDate",defaultValue = "")String nowDate){
        if (StringUtils.isBlank(fuserid)){
            fuserid = GetCurrentUserUtils.getCurrentUserId();
        }
        return HttpResult.ok(spcyMyInfoService.getJcrc(fuserid, nowDate));
    }

    @Log(title = "检查员身份变更记录")
    @ApiOperation(value = "检查员身份变更记录")
    @GetMapping(value = "getJcysfbgjl")
    public HttpResult getJcysfbgjl(@RequestParam(value = "fuserid",defaultValue = "")String fuserid){
        if (StringUtils.isBlank(fuserid)){
            fuserid = GetCurrentUserUtils.getCurrentUserId();
        }
        SpcyRsJcysfbgjl query = new SpcyRsJcysfbgjl();
        query.setFuserid(fuserid);
        return HttpResult.ok(spcyRsJcysfbgjlService.selectAllList(query,new OrderItemVo()));
    }

    @Log(title = "个人选派记录")
    @ApiOperation(value = "个人选派记录")
    @GetMapping(value = "getGrxpjlList")
    public HttpResult getGrxpjlList(SpcySqsxXpjl query, PageVo pageVo, OrderItemVo orderItemVo){
        boolean onlyCurrentUser = false;
        if (StringUtils.isBlank(query.getFuserid())){
            onlyCurrentUser = true;
        }
        query.setFsftyxp(SpcyConstant.XPJL_SFTYXP_YES);
        return HttpResult.ok(spcySqsxXpjlService.selectPageList(query,pageVo,orderItemVo,onlyCurrentUser));
    }

    @Log(title = "年度考评记录")
    @ApiOperation(value = "年度考评记录")
    @GetMapping(value = "getGrNdkpList")
    public HttpResult getGrNdkpList(SpcyKpNdkpBasic query, PageVo pageVo, OrderItemVo orderItemVo){
        boolean onlyCurrentUser = false;
        if (StringUtils.isBlank(query.getFuserid())){
            onlyCurrentUser = true;
        }
        return HttpResult.ok(spcyKpNdkpBasicService.selectPageList(query, pageVo, orderItemVo, onlyCurrentUser));
    }

    @ApiOperation(value = "检查员学历职称分布情况")
    @GetMapping(value = "jcyxlzcfbqk")
    public HttpResult jcyxlzcfbqk(){

        return HttpResult.ok(spcyMyInfoService.jcyxlzcfbqk());
    }

    @ApiOperation(value = "检查员类别分布情况")
    @GetMapping(value = "jcylbfbqk")
    public HttpResult jcylbfbqk(){

        return HttpResult.ok(spcyMyInfoService.jcylbfbqk());
    }

    @ApiOperation(value = "检查员个人相关-流程待办数量")
    @GetMapping("flowDbSizeMap")
    public HttpResult flowDbSizeMap(PageVo pageVo){

        return HttpResult.ok(spcyMyInfoService.getXcjcKpDbsl(GetCurrentUserUtils.getCurrentUserId(), pageVo));
    }

    @ApiOperation(value = "验证登录用户-银行卡/电子签名/头像是否存在")
    @GetMapping(value = "verifyByYkhQzdmTx")
    public HttpResult verifyByYkhQzdmTx(){

        return HttpResult.ok(spcyMyInfoService.verifyByYkhQzdmTx());
    }

    @ApiOperation(value = "验证登录用户-是否为科室负责人")
    @GetMapping(value = "verifyUserKsfzr")
    public HttpResult verifyUserKsfzr(){

        return HttpResultVo.HttpResultSave(spcyMyInfoService.getUserKsfzrData(GetCurrentUserUtils.getCurrentUserId()));
    }

    @ApiOperation(value = "获取经办人-我的一年份任务量")
    @GetMapping(value = "getJbrWdynfrwl")
    public HttpResult getJbrWdYearrwl(@RequestParam(value = "year",defaultValue = "")String year){

        return HttpResult.ok(spcyMyInfoService.getNowYearSpcyRwlInStat(GetCurrentUserUtils.getCurrentUserId(), true, year));
    }

//    @ApiOperation(value = "获取经办人-我的半年份任务量")
    @GetMapping(value = "getJbrWdbnfrwl")
    public HttpResult getJbrWdbYearrwl(@RequestParam(value = "year",defaultValue = "")String year){

        return HttpResult.ok(spcyMyInfoService.getNowYearSpcyRwlInStat(GetCurrentUserUtils.getCurrentUserId(), true, year));
    }

    @ApiOperation(value = "获取经办人-我的一个月任务量")
    @GetMapping(value = "getJbrWdmonthrwl")
    public HttpResult getJbrWdmonthrwl(){

        return HttpResult.ok(spcyMyInfoService.countJobsPerWeekInMonthsMainNew(GetCurrentUserUtils.getCurrentUserId()));
    }

    @ApiOperation(value = "获取本月本科室-人员任务量")
    @GetMapping(value = "getbksRyrwl")
    public HttpResult getbksRyrwl(@RequestParam(value = "kssj",defaultValue = "") String kssj,
                                  @RequestParam(value = "jssj",defaultValue = "") String jssj){

        return HttpResult.ok(spcyMyInfoService.getNowUserKsryRwlInStat(GetCurrentUserUtils.getCurrentUserOrgId(), kssj, jssj));
    }

    @ApiOperation(value = "获取本科室-各环节任务量")
    @GetMapping(value = "getbksHjmcRwl")
    public HttpResult getbksHjmcRwl(){

        return HttpResult.ok(spcyMyInfoService.getNowUserKsHjmcRwlInStat());
    }

    @ApiOperation(value = "专职人员派检率（中心人员参见到检查的次数/每月的检查次数）")
    @GetMapping(value = "getZzrypjl")
    public HttpResult getZzrypjl(@RequestParam(value = "year",defaultValue = "") String year){

        return HttpResult.ok(spcyMyInfoService.zzRypjl(GetCurrentUserUtils.getCurrentUserId(), year));
    }

    @ApiOperation(value = "获取检查员考评相关待办数据-根据事项")
    @GetMapping(value = "kpdbList")
    public HttpResult kpdbList(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        return HttpResult.ok(spcyMyInfoService.kpdbListByUserIdAndSqsxId(sqsxid, ""));
    }

}
